////////////////////////////////////////////////////////////////////////////////
//  COPYRIGHT (C) 2010 TELECOM DIGITAL MEDIA ("TDMEDIA").
//  ALL RIGHTS RESERVED.
//
//  THIS IS CONFIDENTIAL AND PROPRIETARY INTELLECTUAL PROPERTY OWNED BY AND
//  CREATED ON BEHALF OF TDMEDIA. ANY FORM OF DISTRIBUTION, COPY,
//  MODIFICATION WITHOUT THE WRITTEN CONSENT FROM TDMEDIA IS STRICTLY
//  PROHIBITED.
////////////////////////////////////////////////////////////////////////////////
package cc.telecomdigital.tdstock.activity.groups.more;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import cc.telecomdigital.tdstock.CommonDefn;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.BaseActivity;
import cc.telecomdigital.tdstock.activity.groups.MoreGroup;
import cc.telecomdigital.tdstock.activity.groups.more.push.client.Constants;
import cc.telecomdigital.tdstock.activity.groups.more.push.client.Notifier;

/**
 * 
 * @author haibo
 * 
 *  for button   named  "setting"
 *
 */

public class MoreSettingActivity extends BaseActivity
{

	protected static final String	TAG	= MoreSettingActivity.class
										.getSimpleName();
	
	//private String[]	NOTIFICATION_TYPES	= new String[] { " 顯示未讀條目總數",	"在狀態欄顯示其內容"	};
	

//	public static final int  SET_RINGTONG = 999;

	private Button				_toolBtnReturn;

	private CheckBox			_isAutoLogin;
	private SharedPreferences	_prefse;

	//private View				autoLoginSetting;
    
	private CheckBox   _push_service;                    //警报服务提醒总开关
	//private CheckBox  _push_sound;                      //警报声音提醒开关
	private CheckBox _push_vibrate;                      //警报震动提醒开关
	
//	private RelativeLayout  _auto_push_service;   //警报服务提醒总开关
//	private RelativeLayout  _auto_push_sound;   //警报声音提醒开关
//	private RelativeLayout  _auto_push_vibrate;   //警报震动提醒开关
	
	private Button _btnNotificationShowStyle;
	private Button _btnNotificationChooseRington;
	
	
	private final static  boolean isInTouchMode  = true;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.more_setting);
		resetMinHeightOnWorkCenter();
		createSharePreference();
		_toolBtnReturn = (Button) findViewById(R.id.more_returnbtn);
		_toolBtnReturn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				TDStockLog.d(TAG, "close current activity and forwarding to the previous");

				Intent intent = new Intent(getApplicationContext(),MoreActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
				
				SwitchForwardActivity(MoreActivity.class, intent);
			}

		});

		_isAutoLogin = (CheckBox) findViewById(R.id.auto_login_app);

	
         /*hunter 2011-10-14 */
		_push_service=(CheckBox)findViewById(R.id.push_service);
	//	_push_sound=(CheckBox)findViewById(R.id.push_sound);
		_push_vibrate=(CheckBox)findViewById(R.id.push_vibrate);
//		_auto_push_service=(RelativeLayout)findViewById(R.id.more_setting_auto_push_service);
//		_auto_push_sound=(RelativeLayout)findViewById(R.id.more_setting_auto_push_sound);
//		_auto_push_vibrate=(RelativeLayout)findViewById(R.id.more_setting_auto_push_vibrate);
		//set check  boolean
		SharedPreferences sharedPreferences = MoreSettingActivity.this.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		_push_service.setChecked(sharedPreferences.getBoolean(Constants.SETTINGS_NOTIFICATION_ENABLED, true));
		//_push_sound.setChecked(sharedPreferences.getBoolean(Constants.SETTINGS_SOUND_ENABLED, true));
		_push_vibrate.setChecked(sharedPreferences.getBoolean(Constants.SETTINGS_VIBRATE_ENABLED, true));
		
		//event
		_push_service.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				MoreSettingActivity.this.serviceEvent(isChecked);
				
				_btnNotificationChooseRington.setEnabled(isChecked);
				
				_btnNotificationChooseRington.setEnabled(isChecked);
				_btnNotificationShowStyle.setEnabled(isChecked);
				_push_vibrate.setEnabled(isChecked);
				
			}
		});
		
	/*	_push_sound.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				MoreSettingActivity.this.soundEvent(isChecked);
			}
		});*/
		
		_push_vibrate.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				MoreSettingActivity.this.vibrateEvent(isChecked);
			}
		});
		
//		_auto_push_service.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				SharedPreferences sharedPreferences = MoreSettingActivity.this.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
//				boolean _flag_service=sharedPreferences.getBoolean(Constants.SETTINGS_NOTIFICATION_ENABLED,true);
//				_push_service.setChecked(_flag_service?false:true);
//				MoreSettingActivity.this.serviceEvent(_flag_service?false:true);
//			}
//		});
//		_auto_push_sound.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				SharedPreferences sharedPreferences = MoreSettingActivity.this.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
//				boolean _flag_sound=sharedPreferences.getBoolean(Constants.SETTINGS_SOUND_ENABLED,true);
//				_push_sound.setChecked(_flag_sound?false:true);
//				MoreSettingActivity.this.soundEvent(_flag_sound?false:true);
//			}
//		});
//		_auto_push_vibrate.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				SharedPreferences sharedPreferences = MoreSettingActivity.this.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
//				boolean _flag_vibrate=sharedPreferences.getBoolean(Constants.SETTINGS_VIBRATE_ENABLED,true);
//				_push_vibrate.setChecked(_flag_vibrate?false:true);
//				MoreSettingActivity.this.vibrateEvent(_flag_vibrate?false:true);
//			}
//		});
		
		/*hunter  2011-10-14*/
		
		
		_btnNotificationShowStyle = (Button)findViewById(R.id.notification_show_multiple);
		
		_btnNotificationChooseRington = (Button)findViewById(R.id.notification_btn_ringtong);
		
		_btnNotificationShowStyle
		.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				final SharedPreferences sharedPreferences = MoreSettingActivity.this.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
				
				
				final int theDefaultSetting = 	sharedPreferences.getInt(CommonDefn.PREFERENCE_NOTIFICATION_SHOW_TYPE_ID, Notifier.SHOW_NOTIFICATION_MULTIPL);
				
				
				new AlertDialog.Builder(MoreSettingActivity.this.getParent())
						.setTitle(R.string.notification_show_style)
						.setSingleChoiceItems(R.array.notification_show_style, theDefaultSetting,new DialogInterface.OnClickListener()
						{
							@Override
							public void onClick(
									DialogInterface dialog,
									int which)
							{
							
								sharedPreferences.edit().putInt(
										CommonDefn.PREFERENCE_NOTIFICATION_SHOW_TYPE_ID,
										which).commit();
								
								dialog.dismiss();
								
							}
						}).show();
			}
		});
		
		
		_btnNotificationChooseRington
		.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				
				
				Intent intent = new Intent( RingtoneManager.ACTION_RINGTONE_PICKER);
				intent.putExtra( RingtoneManager.EXTRA_RINGTONE_TYPE,
				RingtoneManager.TYPE_NOTIFICATION);
				
				Uri  ringtongUri = null;
				final SharedPreferences sharedPreferences = MoreSettingActivity.this.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
				
				String uri = sharedPreferences.getString(CommonDefn.PREFERENCE_NOTIFICATION_RINGTONG, "null");
				if("null".equals(uri)) {
					ringtongUri  = (Uri) null;
				}
				else {
					ringtongUri = android.net.Uri.parse(uri);
				}
				
				intent.putExtra( RingtoneManager.EXTRA_RINGTONE_TITLE, getString(R.string.notification_ringtong));
			
				
				intent.putExtra( RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,ringtongUri);
				
				MoreSettingActivity.this.getParent().startActivityForResult( intent, RESULT_FIRST_USER);   //must start the activity with parent activity, else  can't get the response.
				
			}
		
			});
		
		
		
		initTouchMode();
		initSettingItems();
		
		
	}

	private void updateIsAutoLogin(boolean isSave)
	{
		_prefse = getSharedPreferences(CommonDefn.SYSTEM_CURRENT_SETTING,
				Context.MODE_PRIVATE);
		if (_prefse != null)
		{

			if (!isSave)
			{ //just read
				_isAutoLogin.setOnCheckedChangeListener(null); //do not  save to xml file

				boolean isAutoLogin = _prefse.getBoolean(
						CommonDefn.PREFERENCE_IS_AUTO_LOGIN_APP, true);
				_isAutoLogin.setChecked(isAutoLogin);
				_isAutoLogin.invalidate();

				//reset update handler
				_isAutoLogin
						.setOnCheckedChangeListener(new OnCheckedChangeListener()
						{
							@Override
							public void onCheckedChanged(
									CompoundButton buttonView,
									boolean isChecked)
							{
								updateIsAutoLogin(true);
							}
						});
			}
			else
			{
				_prefse.edit().putBoolean(
						CommonDefn.PREFERENCE_IS_AUTO_LOGIN_APP,
						_isAutoLogin.isChecked()).commit();
			}
		}
	}

	@Override
	protected void onPause()
	{
		super.onPause();
	}

	@Override
	protected void onRestart()
	{
		super.onRestart();
	}

	@Override
	protected void onResume()
	{
		super.onResume();

		updateIsAutoLogin(false);
		
	}

	private void forwardToHorseraceBettingActivity()
	{

		TDStockLog.d(TAG, "forwardToHorseraceBettingActivity");
		Intent intent = new Intent(getApplicationContext(),
				HorseracePhoneBettingActivity.class);
		intent.setFlags( Intent.FLAG_ACTIVITY_NO_USER_ACTION);
		
		SwitchForwardActivity(HorseracePhoneBettingActivity.class, intent);

	}

	private void forwardToSoccerBettingActivity()
	{

		TDStockLog.d(TAG, "forwardToHSoccerBettingActivity");

		Intent intent = new Intent(getApplicationContext(),
				SoccerPhoneBettingActivity.class);
		intent.setFlags( Intent.FLAG_ACTIVITY_NO_USER_ACTION);
		
		SwitchForwardActivity(SoccerPhoneBettingActivity.class, intent);
		
	}
	
	@Override
	public ActivityGroupController GetActivityGroupController()
	{
		return MoreGroup.GetActivityGroupHostControl();
	}
	/*huter   2011-10-17*/
	/**
	 * create a share preference
	 */
	public void createSharePreference(){
		SharedPreferences sharedPreferences = this.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		String flag=sharedPreferences.getString(Constants.FLAG, null);
		if(flag==null){
		SharedPreferences.Editor editor=sharedPreferences.edit();
		editor.putString(Constants.FLAG, "flag");
		editor.putBoolean(Constants.SETTINGS_NOTIFICATION_ENABLED, true);
		editor.putBoolean(Constants.SETTINGS_SOUND_ENABLED, true);
		editor.putBoolean(Constants.SETTINGS_VIBRATE_ENABLED, true);
		editor.putBoolean(Constants.SETTINGS_TOAST_ENABLED, false);
		editor.commit();
		}
	}
	// _push_service  checkBox event
   private void serviceEvent(boolean flag){
	   SharedPreferences sharedPreferences = MoreSettingActivity.this.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor=sharedPreferences.edit();
		editor.putBoolean(Constants.SETTINGS_NOTIFICATION_ENABLED, flag);
//		if(!flag){
//		_push_sound.setChecked(flag);
//		_push_vibrate.setChecked(flag);
//		editor.putBoolean(Constants.SETTINGS_SOUND_ENABLED, flag);
//		editor.putBoolean(Constants.SETTINGS_VIBRATE_ENABLED, flag);
//		}
		editor.commit();
   }
// _push_sound  checkBox event
   private void soundEvent(boolean flag){
	   SharedPreferences sharedPreferences =MoreSettingActivity.this.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor=sharedPreferences.edit();
		editor.putBoolean(Constants.SETTINGS_SOUND_ENABLED, flag);
		editor.commit();
		
   }
// _push_vibrate  checkBox event
   private void vibrateEvent(boolean flag){
	   SharedPreferences sharedPreferences = MoreSettingActivity.this.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor=sharedPreferences.edit();
		editor.putBoolean(Constants.SETTINGS_VIBRATE_ENABLED, flag);
		editor.commit();
   }
	/*huter   2011-10-17*/

/*@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	//it don't work here for it is in tabhost, so let the parent work!
TDStockLog.d(TAG, String.format("requestCode=%d,resultCode = %d", requestCode,  resultCode));
		
		if (resultCode == RESULT_OK ) {
			Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
			if (uri != null) {
					String ringTonePath = uri.toString();
					TDStockLog.d(TAG, ringTonePath);
					SharedPreferences	_prefse  = getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
					
					_prefse.edit().putString(CommonDefn.PREFERENCE_NOTIFICATION_RINGTONG, ringTonePath).commit();
					
					
				}
				else {
			TDStockLog.d(TAG, "null");
			SharedPreferences	_prefse  = getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
			
			_prefse.edit().putString(CommonDefn.PREFERENCE_NOTIFICATION_RINGTONG, "null").commit();
		}
			}
		
		
		super.onActivityResult(requestCode, resultCode, data);
	}
	*/





private void  setTouch(final View currentView,  int parentLevel) {
	View parent = null;
	if( currentView == null  ) {
		return;
	}
	
	for(int level = 0; level < parentLevel; level++) {
		 ViewParent  container = currentView.getParent();
		 if(container == null)  {
			 break;
		 }else {
			 parent = (View)container;
		 }
		
	}
	
	if(parent == null) {
		return;
	}
	
	 parent.setOnClickListener(  new  OnClickListener() {

		@Override
		public void onClick(View v) {
				currentView.performClick();
			
		}
		 
	 });
	
	
}
   

	private void initTouchMode() {
		if(!isInTouchMode) {
			return;
		}
		
		setTouch(_btnNotificationChooseRington, 1);
		setTouch(_btnNotificationShowStyle, 1);
		setTouch(_push_vibrate, 1);
		setTouch(_push_service, 1);
		setTouch(_isAutoLogin, 1);
	}
   
	
	private void initSettingItems() {
		ViewGroup  notificationSettings = (ViewGroup)findViewById(R.id.notification_setting_id);
		if(notificationSettings == null) {
			return;
		}
		
		if(  ((ITDLApplication)this.getApplication()).getDataMode() == CommonDefn.DataMode.Delay) {
			notificationSettings.setVisibility(View.GONE);
		}
		else {
			notificationSettings.setVisibility(View.VISIBLE);
		}
		
		
		
		
		
	}
   
   
   
   
}
